import Vue from 'vue'
import Router from 'vue-router'

import page from '@/components/page'
import cart from '@/components/cart'
import classify from '@/components/classify'
import my_home from '@/components/my_home'
import detail from '@/components/detail'
import register from '@/components/register'
import login from '@/components/login'
import test from '@/components/test'
import change_info from '@/components/change_info'
import Swipdetail from '@/components/swipdetail'
import Seek from '@/components/Seek'




Vue.use(Router)

var router = new Router({
    routes: [{
            path: '/',
            name: 'page',
            component: page
        },
        {
            path: "/page",
            name: "page",
            component: page
        },
        {
            path: "/classify",
            name: "classify",
            component: classify
        },
        {
            path: "/cart",
            name: "cart",
            component: cart
        },
        {
            path: "/my_home",
            name: "my_home",
            component: my_home
        },
        {
            path: "/detail",
            name: "detail",
            component: detail
        }, {
            path: "/register",
            name: "register",
            component: register,
        },
        {
            path: "/login",
            name: "login",
            component: login
        }, {
            path: "/test",
            name: "test",
            component: test
        }, {
            path: "/change_info",
            name: "change_info",
            component: change_info
        }, {
            path: "/swipdetail",
            name: "Swipdetail",
            component: Swipdetail
        }, {
            path: "/seek",
            name: "Seek",
            component: Seek
        }
    ]
})
router.beforeEach((to, from, next) => {
    // to and from are both route objects. must call `next`.
    if (to.path == "/register" || to.path == "/login" || to.path == "/test" || to.path == "change_info") {
        next();
    } else {
        if (sessionStorage.getItem("token")) {
            next();
        } else {
            next("/login");
        }
    }
})
export default router;